<template>
  <div>
    <Header :left="true" leftText="返回" title="调机审核" />

    <!-- 滑动区域，适应手机分辨率 -->
    <div class="textScroll">
      <!-- 文字信息 -->
      <van-row style="margin-top: 10px">
        <van-col>
          <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
        </van-col>
        <van-col class="message" style="margin-top: 5px">调机单据信息</van-col>
      </van-row>

      <div class="divMessage">
        <Field v-model="form.Id" :readonly="true" size="small" class="Field" center colon label="调机单ID" />
        <Field v-model="form.ProductionDate" rows="1" autosize style="font-weight: bold; color: #a9a9a9" :readonly="true"
          label="生产日期" type="textarea" />
        <Field v-model="form.POrderID" :readonly="true" size="small" class="Field" center colon label="生产订单" />
        <Field v-model="form.MaterialID" :readonly="true" size="small" class="Field" center colon label="物料编号" />
        <Field v-model="form.MaterialName" :readonly="true" size="small" class="Field" center colon label="物料名称" />
        <Field :readonly="true" size="small" class="Field" center colon label="生产时间">
          <template #input>
            <span>从 {{ form.ProDatetimeFrom }} 至 {{ form.ProDatetimeTo }}</span>
          </template>
        </Field>
        <Field v-model="form.QProblemID" :readonly="true" type="textarea" rows="4" class="Field" label="异常原因">

        </Field>
        <Field v-model="form.ManageReasonID" :readonly="true" type="textarea" rows="4" class="Field" label="       ">

        </Field>
        <Field v-model="form.FunctionProblemID" :readonly="true" type="textarea" rows="4" class="Field" label="       ">

        </Field>
        <Field v-model="form.NoticeMemo" :readonly="true" size="small" class="Field" center colon label="通知单描述" />
        <Field v-model="form.ProductionBatch" :readonly="true" size="small" class="Field" center colon label="生产批次" />
        <Field :readonly="true" size="small" class="Field" center colon label="班次">
          <template #input>
            {{ showShiftIDName(form.PShiftID) }}
          </template>
        </Field>
        <Field v-model="form.MaterialSpec" :readonly="true" size="small" class="Field" center colon label="物料规格" />
        <Field v-model="form.MachineID" :readonly="true" size="small" class="Field" center colon label="设备代码" />
        <Field v-model="form.SampleAmount" :readonly="true" size="small" class="Field" center colon label="抽样数量" />
        <Field v-model="form.AbnormalAmount" :readonly="true" size="small" class="Field" center colon label="异常数量" />
        <Field v-model="result" :readonly="true" size="small" class="Field" center colon label="质量要求" />
        <Field v-model="form.Reasons" :readonly="true" size="small" class="Field" center colon label="需注明的缺陷描述" />
        <Field v-model="form.Creator" :readonly="true" size="small" class="Field" center colon label="调机申请QC" />
        <Field v-model="form.CreateDatetime" :readonly="true" size="small" class="Field" center colon label="创建时间" />
      </div>
      <!-- 品管领班审核 -->
      <div v-if="status >= 1">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
          </van-col>
          <van-col class="message" style="margin-top: 5px">品管领班审核</van-col>
        </van-row>

        <Field name="是否同意" class="formstyle" label="是否同意" required>
          <template #input>
            <van-radio-group v-model="QCLeaderAgreeValue" direction="horizontal" @change="changeAgreeValue">
              <van-radio name="1">同意</van-radio>
              <van-radio name="0">不同意</van-radio>
            </van-radio-group>
          </template>
        </Field>

        <Field class="formstyle" v-model="form.QCLeaderMemo" required name="处置建议" label="处置建议" placeholder="输入品管领班处置建议"
          rows="2" autosize type="textarea" maxlength="100" show-word-limit />
        <van-field v-if="showEmployee" required readonly class="formstyle" name="picker" :value="EmployeeName"
          label="生产现场主管审核" placeholder="点击选择生产现场主管" @click="showPickerEmployee = true" />
        <van-popup v-model="showPickerEmployee" position="bottom">
          <van-picker show-toolbar :columns="columnsEmployee" @confirm="onConfirmEmployee"
            @cancel="showPickerEmployee = false">
            <template #option="option">
              <div style="display: flex; flex-direction: column; align-items: center;">
                <div>{{ option.EmployeeName }}</div>
              </div>
            </template>
          </van-picker>
        </van-popup>
        <van-field required readonly class="formstyle" label="审核时间" :value="nowDate" />
      </div>
      <!-- 生产现场主管审核 -->
      <div v-if="status >= 2">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
          </van-col>
          <van-col class="message" style="margin-top: 5px">生产现场主管审核</van-col>
        </van-row>

        <Field name="是否同意" class="formstyle" label="是否同意" required>
          <template #input>
            <van-radio-group v-model="TeamLeaderAgreeValue" direction="horizontal" @change="changeAgreeValue">
              <van-radio name="1">同意</van-radio>
              <van-radio name="0">不同意</van-radio>
            </van-radio-group>
          </template>
        </Field>
        <Field class="formstyle" v-model="form.TeamLeaderMemo" required name="处置建议" label="处置建议"
          placeholder="输入生产现场主管处置建议" rows="2" autosize type="textarea" maxlength="100" show-word-limit />
        <van-field v-if="showEmployee" required readonly class="formstyle" name="picker" :value="EmployeeName"
          label="生产部门负责人审核" placeholder="点击选择生产部门负责人" @click="showPickerEmployee = true" />
        <van-popup v-model="showPickerEmployee" position="bottom">
          <van-picker show-toolbar :columns="columnsEmployee" @confirm="onConfirmEmployee"
            @cancel="showPickerEmployee = false">
            <template #option="option">
              <div style="display: flex; flex-direction: column; align-items: center;">
                <div>{{ option.EmployeeName }}</div>
              </div>
            </template>
          </van-picker>
        </van-popup>
        <van-field required readonly class="formstyle" label="审核时间" :value="nowDate" />
      </div>
      <!-- 生产部门负责人审核 -->
      <div v-if="status >= 3">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
          </van-col>
          <van-col class="message" style="margin-top: 5px">生产部门负责人审核</van-col>
        </van-row>

        <Field name="是否同意" class="formstyle" label="是否同意" required>
          <template #input>
            <van-radio-group v-model="PrdManagerAgreeValue" direction="horizontal" @change="changeAgreeValue">
              <van-radio name="1">同意</van-radio>
              <van-radio name="0">不同意</van-radio>
            </van-radio-group>
          </template>
        </Field>
        <Field class="formstyle" v-model="form.PrdMangerMemo" required name="处置建议" label="处置建议"
          placeholder="输入生产部门负责人处置建议" rows="2" autosize type="textarea" maxlength="100" show-word-limit />
        <van-field v-if="showEmployee" required readonly class="formstyle" name="picker" :value="EmployeeName"
          label="质量负责人审核" placeholder="点击选择质量负责人" @click="showPickerEmployee = true" />
        <van-popup v-model="showPickerEmployee" position="bottom">
          <van-picker show-toolbar :columns="columnsEmployee" @confirm="onConfirmEmployee"
            @cancel="showPickerEmployee = false">
            <template #option="option">
              <div style="display: flex; flex-direction: column; align-items: center;">
                <div>{{ option.EmployeeName }}</div>
              </div>
            </template>
          </van-picker>
        </van-popup>
        <van-field required readonly class="formstyle" label="审核时间" :value="nowDate" />
      </div>
      <!-- 质量负责人审核 -->
      <div v-if="status >= 4">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
          </van-col>
          <van-col class="message" style="margin-top: 5px">质量负责人审核</van-col>
        </van-row>

        <Field name="是否同意" class="formstyle" label="是否同意" required>
          <template #input>
            <van-radio-group v-model="QCManagerAgreeValue" direction="horizontal" @change="changeAgreeValue">
              <van-radio name="1">同意</van-radio>
              <van-radio name="0">不同意</van-radio>
            </van-radio-group>
          </template>
        </Field>
        <Field class="formstyle" v-model="form.QCMangerMemo" required name="处置建议" label="处置建议" placeholder="输入质量负责人处置建议"
          rows="2" autosize type="textarea" maxlength="100" show-word-limit />
        <van-field v-if="showEmployee" required readonly class="formstyle" name="picker" :value="EmployeeName"
          label="利润点负责人审核" placeholder="点击选择利润点负责人" @click="showPickerEmployee = true" />
        <van-popup v-model="showPickerEmployee" position="bottom">
          <van-picker show-toolbar :columns="columnsEmployee" @confirm="onConfirmEmployee"
            @cancel="showPickerEmployee = false">
            <template #option="option">
              <div style="display: flex; flex-direction: column; align-items: center;">
                <div>{{ option.EmployeeName }}</div>
              </div>
            </template>
          </van-picker>
        </van-popup>
        <van-field required readonly class="formstyle" label="审核时间" :value="nowDate" />
      </div>
      <!-- 利润点负责人审核 -->
      <div v-if="status >= 5">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image width="40" height="30" style="margin-left: -10px" src="assets/block.png" />
          </van-col>
          <van-col class="message" style="margin-top: 5px">利润点负责人审核</van-col>
        </van-row>

        <Field name="是否同意" class="formstyle" label="是否同意" required>
          <template #input>
            <van-radio-group v-model="FactoryManagerAgreeValue" direction="horizontal" @change="changeAgreeValue">
              <van-radio name="1">同意</van-radio>
              <van-radio name="0">不同意</van-radio>
            </van-radio-group>
          </template>
        </Field>
        <Field class="formstyle" v-model="form.FactoryMangerMemo" required name="处置建议" label="处置建议"
          placeholder="输入利润点负责人处置建议" rows="2" autosize type="textarea" maxlength="100" show-word-limit />
        <van-field required readonly class="formstyle" label="审核时间" :value="nowDate" />
      </div>

      <div style="margin: 16px">
        <van-button color="#138087" round block type="info" native-type="submit" @click="submitCheck()">确认提交</van-button>
      </div>

    </div>




    <!-- 底部按钮 -->
    <!--<van-row class="button" gutter="2">
      <van-col span="8">
        <van-button
          :block="true"
          @click="startRepair"
          :disabled="btnStartShow"
          class="buttonColor2"
          type="primary"
          color="#138087"
          text="调机开始"
        />
      </van-col>
      <van-col span="8">
        <van-button
          :block="true"
          class="buttonColor"
          @click="stopRepair"
          type="primary"
          color="#f47920"
          text="暂停调机"
        />
      </van-col>
      <van-col span="8">
        <van-button
          :block="true"
          class="buttonColor2"
          @click="finishRepair"
          type="primary"
          color="#ed1941"
          text="调机结束"
        />
      </van-col>
    </van-row>-->
    <!-- 故障类型弹窗 -->
    <van-popup v-model="faultShowPicker" round position="bottom">
      <van-picker showPicker="false" title="故障类型选择" value-key="text" show-toolbar @confirm="faultOnConfirm"
        :columns="faultTypes" @cancel="faultShowPicker = false" default-index="0" />
    </van-popup>

    <!-- 暂停原因 -->
    <van-popup v-model="resonShow" round position="bottom">
      <van-picker showPicker="false" title="暂停原因选择" value-key="text" show-toolbar @confirm="resonConfirm"
        :columns="stopReson" @cancel="resonShow = false" default-index="0" />
    </van-popup>
  </div>
</template>
<script>
import {
  Icon,
  NoticeBar,
  Button,
  Row,
  Tag,
  Checkbox,
  Pagination,
  popup,
  Loading,
  Popup,
  Panel,
  ImagePreview,
  Toast,
  tab,
  tabs,
  Divider,
  Image as VanImage,
  Field,
  CellGroup,
  Cell,
  Picker,
  Uploader,
  Dialog,
  Col,
  RadioGroup,
  Radio,
} from "vant";
import { _httpClient as _httpClientApi } from "@/libs/api";
import { _httpClient } from "@/libs/apisie";
import { storage } from "@/libs/utils";
import {
  removeToken,
  getToken,
  setBaseApiFrefix,
  getBaseApiFrefix,
} from "@/libs/auth";
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(tab);
Vue.use(tabs);
Vue.use(Button);
Vue.use(Col);
Vue.use(Row);
Vue.use(Icon);
Vue.use(VanImage);
Vue.use(Divider);
Vue.use(Checkbox);
Vue.use(Pagination);
Vue.use(Panel);
Vue.use(Dialog);
Vue.use(Picker);
Vue.use(popup);
Vue.use(Uploader);
Vue.use(Loading);
Vue.use(Field);
Vue.use(RadioGroup);
Vue.use(Radio);
import Header from "@/components/header.vue";
import apicloud from "@/libs/apicloud";
export default {
  name: "QCmachineconfirmItem",
  components: {
    Button,
    NoticeBar,
    Header,
    ImagePreview,
    Icon,
    Row,
    tab,
    popup,
    Popup,
    tabs,
    Tag,
    VanImage,
    Toast,
    Divider,
    Field,
    CellGroup,
    Cell,
    Uploader,
    Picker,
    Dialog,
    Col,
    RadioGroup,
    Radio,
  },

  data() {
    return {
      btnLoading: false,
      winName: "", //窗口名
      user: [], //当前登录用户
      //多媒体文件
      photoList: [
        //图片
      ],
      pictureNum: 0, //照片数量
      playing: "播放中...", //语音播放中字幕
      audioLoad: false, // 语音播放中
      audioStop: true, // 语音暂停
      photoShow: false, //照片显示控制
      vedioShow: false, //视频显示控制
      audioShow: false, //语音显示控制
      vedioList: [], //视频
      audioPath: "", //语音
      resonShow: false, //暂停原因录入弹窗是否显示
      btnStartShow: false, //开始调机按钮权限
      btnAssignShow: true, //任务转派按钮权限
      btnStopShow: true, //调机暂停按钮权限
      btnEndShow: true, //调机结束按钮权限
      stopResonValue: "", //调机暂停原因

      equipRepairVo: {
        id: null,
        eqKey: null,
        equipName: "",
        equipCode: "",
        remark: null,
        picList: [],
        videoList: [],
        audioList: [],
        resTime: null,
        requestUser: "",
        repairReson: "",
      },
      faultShowPicker: false, //故障原因下拉框
      screenWidth: 0,
      checked: true,
      active: 2,
      helpUser: [], //协助人
      helpUserStr: "", //协助人拼接字符串
      helpUserNameStr: "", //协助人拼接字符串
      helpUserContent: "", //协助人拼接字段
      faultType: "", //故障类型
      sparePart: [], //备件记录
      sparePartContent: "", //备件拼接字段
      fileUrl: "",
      form: [],
      faultTypes: [],
      stopReson: [],
      uploadSuccess: false,
      columnsDealType: [],
      columnsEmployee: [],
      columnsQProblem: [],
      columnsshift: [],
      showPickerEmployee: false,
      showPickerQProblem: false,
      item: {},
      valuepDealTypeName: "",
      EmployeeName: "",
      time: "",
      FactoryID: "",
      valuepShiftName: "",
      CheckTypeName: "",
      QProblemName: "",
      valueteamid: "",
      nowDate: "",
      EmployeeID: "",
      result: [],
      //领班
      QCLeaderAgreeValue: "",
      //带版主管
      TeamLeaderAgreeValue: "",
      //质量负责人
      QCManagerAgreeValue: "",
      //生产部门负责人
      PrdManagerAgreeValue: "",
      //利润点负责人
      FactoryManagerAgreeValue: "",
      //当前状态
      status: "",
      //是否显示人员
      showEmployee: false,
    };
  },
  created: function () {
    let oldHight = api.winWidth;
    this.screenWidth = oldHight;
    this.user = JSON.parse(storage.get("user"));
    this.user.userId = this.user.userName;
    // this.equipRepairVo.resTime = this.getNowTime()
    //页面赋值
    this.form = api.pageParam.item;
    this.nowDate = this.getNowTime();
    this.initList();
    this.getUsername(this.form.Creator)
    // this.winName = api.pageParam.winName
    // this.equipRepairVo.id = this.form.id
    // this.refreshBtn()
    // //初始化查询调机协助人
    // this.queryHelpUser()
    // //初始化查询多媒体信息
    // this.queryFileById()
    // //初始化查询故障信息
    // this.queryEmFault()
    // //初始化查询调机暂停原因
    // this.queryEmStopReson()
    this.getQualityProblems();
    this.getshift();
    //获取员工列表
    this.getEmployeeID();
  },
  methods: {
    async getUsername(id) {

      try {

        let res4 = await _httpClient.get(
          _httpClient.getApiPrefix() + "/Employee/GetList?EmployeeID=" + id)

        if (res4.Data.length > 0) {

          this.form.Creator = res4.Data[0].EmployeeName
          return
        }
      } catch (e) {
        alert(JSOn.stringify(e))
      }
      this.form.Creator = id
    },
    initList() {
      if (this.form.Require1) {
        this.result.push("及时调机整改");
      }
      if (this.form.Require2) {
        this.result.push("停机调整");
      }
      if (this.form.Require3) {
        this.result.push("产品要求返工，发出返工通知单");
      }
      if (this.form.Require4) {
        this.result.push("在线自检挑出");
      }

      //判断审批流程状态
      if (!this.form.QCLeaderChecked) {
        //品管领班未审核
        this.status = 1;
      } else if (this.form.QCLeaderChecked && this.form.QCLeaderAgree && !this.form.TeamLeaderChecked) {//品管领班审核且同意,主管未审核
        //生产现场主管审核
        this.status = 2;
      } else if (this.form.TeamLeaderChecked && !this.form.TeamLeaderAgree && !this.form.PrdManagerChecked) {//生产现场主管已审核且不同意，生产部门负责人未审批
        //生产审核
        this.status = 3;
      } else if (this.form.PrdManagerChecked && !this.form.PrdManagerAgree && !this.form.QCManagerChecked) {//生产部门负责人审核且不同意 质量未审核
        //质量审核
        this.status = 4;
      } else if (this.form.QCManagerChecked && this.form.QCManagerAgree && !this.form.FactoryManagerChecked) {//质量负责人审核且同意 利润点未审批
        //利润点负责人审批
        this.status = 5;
        //alert("111")
      }
    },
    //提交审批
    submitCheck() {
      var body = {};
      body.Id = this.form.Id;
      //判断状态
      if (this.status == 1) {
        body.QCLeaderAgree = this.form.QCLeaderAgree;
        body.QCLeaderMemo = this.form.QCLeaderMemo;
        body.QCLeaderUserID = this.user.userId;
        body.QCLeaderCheckDatetime = this.nowDate;
        body.QCLeaderChecked = true;
        //需要调机 选择生产现场主管
        if (this.form.QCLeaderAgree) {
          body.TeamLeaderUserID = this.EmployeeID
        } else {
          //不需要调机 直接结束     
          //传入终审人
          body.FixedMan = this.user.userId;
          //最终审时间
          body.FixedDatetime = this.nowDate;
          //终审是否需要调机
          body.FixedAgree = false
        }
      } else if (this.status == 2) {//生产现场主管审核
        body.TeamLeaderAgree = this.form.TeamLeaderAgree;
        body.TeamLeaderMemo = this.form.TeamLeaderMemo;
        body.TeamLeaderCheckDatetime = this.nowDate;
        body.TeamLeaderChecked = true;
        //需要调机 直接结束
        if (this.form.TeamLeaderAgree) {
          //传入终审人
          body.FixedMan = this.user.userId;
          //最终审时间
          body.FixedDatetime = this.nowDate;
          //终审是否需要调机
          body.FixedAgree = true
        } else {
          //不需要调机 询问生产部门负责人     
          body.PrdMangerUserID = this.EmployeeID
        }
      } else if (this.status == 3) {//生产部门负责人审核
        body.PrdManagerAgree = this.form.PrdManagerAgree;
        body.PrdMangerMemo = this.form.PrdMangerMemo;
        body.PrdManagerCheckDatetime = this.nowDate;
        body.PrdManagerChecked = true
        //需要调机 直接结束
        if (this.form.PrdManagerAgree) {
          //传入终审人
          body.FixedMan = this.user.userId;
          //最终审时间
          body.FixedDatetime = this.nowDate;
          //终审是否需要调机
          body.FixedAgree = true;
        } else {
          //不需要调机 选择质量负责人   
          body.QCMangerUserID = this.EmployeeID
        }
      } else if (this.status == 4) {//质量负责人审核
        body.QCManagerAgree = this.form.QCManagerAgree;
        body.QCMangerMemo = this.form.QCMangerMemo;
        body.QCManagerCheckDatetime = this.nowDate;
        body.QCManagerChecked = true;
        //需要调机 选择利润点负责人
        if (this.form.QCManagerAgree) {
          body.FactoryMangerUserID = this.EmployeeID
        } else {
          //不需要调机 直接结束              
          //传入终审人
          body.FixedMan = this.user.userId;
          //最终审时间
          body.FixedDatetime = this.nowDate;
          //终审是否需要调机
          body.FixedAgree = false
        }
      } else if (this.status == 5) {//利润点负责人审核
        body.FactoryManagerAgree = this.form.FactoryManagerAgree;
        body.FactoryMangerMemo = this.form.FactoryMangerMemo;
        body.FactoryManagerCheckDatetime = this.nowDate;
        body.FactoryManagerChecked = true;
        //传入终审人
        body.FixedMan = this.user.userId;
        //最终审时间
        body.FixedDatetime = this.nowDate;
        //需要调机 直接结束 
        if (this.form.FactoryManagerAgree) {
          //终审是否需要调机
          body.FixedAgree = true
        } else {
          //不需要调机 直接结束    
          //终审是否需要调机
          body.FixedAgree = false
        }
      }
      alert(JSON.stringify(body))
      this.SaveCheckEntity(body)
    },
    async SaveCheckEntity(body) {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "请稍等...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() + "/QualityAbnormalNotice/SaveEntity",
          {
            body: body,
          }
        );
        if (res.Message != null) {
          //message不为空,有报错信息，打卡失败 暂时这样判断
          Toast.clear();
          Toast.fail("保存失败,请核对信息！");
        } else {
          //打卡成功
          Toast.clear();
          Toast.success("保存成功！");
          setTimeout(function () {
            _this.$ac.closeWin();
          }, 2000);
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    //获取员工列表
    async getEmployeeID() {
      let _this = this;
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/Employee/GetList",
        );
        if (res.HttpCode === "200") {
          let temp = res.Data.map(item => ({ EmployeeID: item.EmployeeID, EmployeeName: item.EmployeeName }))
          temp.forEach(item => {
            _this.columnsEmployee.push(item);
          })
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    onConfirmEmployee(value, index) {
      this.EmployeeID = this.columnsEmployee[index].EmployeeID;
      this.EmployeeName = this.columnsEmployee[index].EmployeeName;
      this.showPickerEmployee = false;
    },
    //获取班次信息
    async getshift() {
      let _this = this;
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/ProductionShift/GetList?factoryID=" + _this.form.FactoryID,
        );
        if (res.HttpCode === "200") {
          for (let index = 0; index < res.Data.length; index++) {
            if (res.Data[index].Actived == false) {
              delete res.Data[index];
            }
          }
          let shiftMap = res.Data.map(item => ({ pShiftName: item.PShiftName, pShiftID: item.PShiftID }))
          _this.columnsshift = shiftMap
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    showShiftIDName(pShiftID) {
      var pShiftName;
      this.columnsshift.forEach(item => {
        if (item.pShiftID === pShiftID) {
          pShiftName = item.pShiftName;

        }
      })
      return pShiftName
    },
    showQProblemID(QProblemID) {
      var QProblemName;
      this.columnsQProblem.forEach(item => {
        if (item.QProblemID === QProblemID) {
          QProblemName = item.QProblemName;
        }
      })
      return QProblemName;
    },
    showDealType(type) {
      var DealTypeName;
      this.columnsDealType.forEach(item => {
        if (item.DealTypeID === type) {
          DealTypeName = item.DealTypeName;
        }
      })
      return DealTypeName;
    },
    changeAgreeValue() {
      //如果合格 隐藏页面 直接提交
      if (this.QCLeaderAgreeValue == "1") {
        this.form.QCLeaderAgree = true;
        this.form.QCLeaderMemo = this.getNowTime() + ":" + "品管领班:" + this.user.employeeName + "同意调机!"
        this.showEmployee = true;

      } else if (this.QCLeaderAgreeValue == "0") {
        //若不同意
        this.form.QCLeaderAgree = false;
        this.form.QCLeaderMemo = this.getNowTime() + ":" + "品管领班:" + this.user.employeeName + "不同意调机!"
        this.showEmployee = false;
      }
      if (this.TeamLeaderAgreeValue == "1") {
        //若主管同意
        this.form.TeamLeaderAgree = true;
        this.form.TeamLeaderMemo = this.getNowTime() + ":" + "生产现场主管:" + this.user.employeeName + "同意调机!"
        this.showEmployee = false;
      } else if (this.TeamLeaderAgreeValue == "0") {
        //若主管不同意
        this.form.TeamLeaderAgree = false;
        this.form.TeamLeaderMemo = this.getNowTime() + ":" + "生产现场主管:" + this.user.employeeName + "不同意调机!"
        this.showEmployee = true;
      }
      if (this.QCManagerAgreeValue == "1") {
        //若质量负责人同意
        this.form.QCManagerAgree = true;
        this.form.QCMangerMemo = this.getNowTime() + ":" + "质量负责人:" + this.user.employeeName + "同意调机!";
        this.showEmployee = true;
      } else if (this.QCManagerAgreeValue == "0") {
        //若质量负责人不同意
        this.form.QCManagerAgree = false;
        this.form.QCMangerMemo = this.getNowTime() + ":" + "质量负责人:" + this.user.employeeName + "不同意调机!";
        this.showEmployee = false;
      }
      if (this.PrdManagerAgreeValue == "1") {
        //若生产部门负责人同意
        this.form.PrdManagerAgree = true;
        this.form.PrdMangerMemo = this.getNowTime() + ":" + "生产部门负责人:" + this.user.employeeName + "同意调机!"
        this.showEmployee = false;
      } else if (this.PrdManagerAgreeValue == "0") {
        //若生产负责不人同意
        this.form.PrdManagerAgree = false;
        this.form.PrdMangerMemo = this.getNowTime() + ":" + "生产部门负责人:" + this.user.employeeName + "不同意调机!"
        this.showEmployee = true;
      }
      if (this.FactoryManagerAgreeValue == "1") {
        //若利润点负责人同意
        this.form.FactoryManagerAgree = true;
        this.form.FactoryMangerMemo = this.getNowTime() + ":" + "利润点负责人:" + this.user.employeeName + "同意调机!"
      } else if (this.FactoryManagerAgreeValue == "0") {
        //若利润点负责不人同意
        this.form.FactoryManagerAgree = false;
        this.form.FactoryMangerMemo = this.getNowTime() + ":" + "利润点负责人:" + this.user.employeeName + "不同意调机!"
      }
    },
    //获取缺陷原因
    async getQualityProblems() {
      let _this = this;
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/QualityProblems/GetList",
        );
        if (res.HttpCode === "200") {
          let temp = res.Data.map(item => ({ QProblemID: item.QProblemID, QProblemName: item.QProblemName }))
          temp.forEach(item => {
            _this.columnsQProblem.push(item);
          })
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    //调机暂停
    stopRepair() {
      Dialog.confirm({
        title: "提示",
        message: "是否确认暂停调机?",
        theme: 'round-button',
      })
        .then(() => {
          //点击确定，继续执行后续操作
          //填写暂停原因
          this.resonShow = true;
          //操作后刷新界面按钮权限
          this.refreshBtn();
        })
        .catch(() => { });
    },

    //开始调机
    startRepair() {
      Dialog.confirm({
        title: "提示",
        message: "是否确认开始调机?",
        theme: 'round-button',
      })
        .then(() => {
          //点击确定，继续执行后续操作
          this.updateRepairRcord();
          //操作后刷新界面按钮权限
          this.refreshBtn();
        })
        .catch(() => { });
    },

    //更新调机单
    async updateRepairRcord() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() +
          "/app/repair/repairInfo/updateRepairStatus",
          {
            body: { id: _this.form.id },
          }
        );
        if (res.code === 200) {
          Toast.success(res.msg);
          _this.form.status = "1"; //更新调机单为执行中
          _this.refreshBtn();
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        alert(JSON.stringify(e));
        Toast.fail(JSON.stringify(e));
      }
    },

    //故障原因下拉框确认
    faultOnConfirm(value) {
      if (value[1] == null || value[1] == "") {
        this.faultType = String(value[0]);
      } else {
        this.faultType = String(value[1]);
      }

      this.faultShowPicker = false;
    },

    //协助人选择
    openSelectUser() {
      let _this = this;
      api.openWin({
        name: "helpUser",
        url: "./maintain-toRepair-helpUser.html",
        type: "fade",
        pageParam: {
          helpUser: _this.helpUser,
          recordId: _this.form.id,
        },
      });
      api.addEventListener(
        {
          name: "resultUser",
        },
        function (ret, err) {
          if (ret.value.resultUser.length != 0) {
            _this.helpUser = ret.value.resultUser;
            if (_this.helpUser.length > 0) {
              _this.helpUserStr = "";
              _this.helpUserNameStr = "";
              for (let i = 0; i < _this.helpUser.length; i++) {
                switch (i) {
                  case 0:
                    _this.helpUserStr += _this.helpUser[i].userCode;
                    _this.helpUserNameStr += _this.helpUser[i].userName;
                    break;
                  default:
                    //默认显示三个
                    if (i < 4) {
                      _this.helpUserStr += "、" + _this.helpUser[i].userCode;
                      _this.helpUserNameStr +=
                        "、" + _this.helpUser[i].userName;
                    }
                }
              }
            }
          }
        }
      );
    },

    //按钮权限刷新
    refreshBtn() {
      //校验当前调机单状态，初始化按钮点击权限
      switch (this.form.status) {
        case "0": //待调机
          this.btnStartShow = false;
          this.btnAssignShow = false;
          this.btnStopShow = true;
          this.btnEndShow = true;
          break;
        case "1": //调机中
          this.btnStartShow = true;
          this.btnAssignShow = true;
          this.btnStopShow = false;
          this.btnEndShow = false;
          break;
        case "2": //调机暂停
          this.btnStartShow = false;
          this.btnAssignShow = false;
          this.btnStopShow = true;
          this.btnEndShow = true;
          break;
        case "3": //调机完成
          this.btnStartShow = true;
          this.btnAssignShow = true;
          this.btnStopShow = true;
          this.btnEndShow = true;
          break;
        case "4": //调机不通过
          this.btnStartShow = false;
          this.btnAssignShow = false;
          this.btnStopShow = true;
          this.btnEndShow = true;
          break;
        case "5": //关闭
          this.btnStartShow = true;
          this.btnAssignShow = true;
          this.btnStopShow = true;
          this.btnEndShow = true;
          break;
        default:
          this.btnStartShow = false;
          this.btnAssignShow = false;
          this.btnStopShow = false;
          this.btnEndShow = false;
      }
    },

    //原因选择确认
    resonConfirm(value) {
      this.resonShow = false;
      this.stopResonValue = value.text;
      this.updateRepairStop();
    },
    //记录调机暂停
    async updateRepairStop() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() +
          "/app/repair/repairRecordTime/updateRecordTime",
          {
            body: {
              recordId: _this.form.id,
              stopReson: _this.stopResonValue,
              type: 0,
            },
          }
        );
        if (res.code === 200) {
          Toast.success(res.msg);
          _this.form.status = "2"; //更新调机单为暂停
          _this.refreshBtn();
          if (_this.stopResonValue == "人员换班") {
            //任务转派
            _this.taskAssign();
          }
          _this.stopResonValue = "";
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        alert(JSON.stringify(e));
        Toast.fail(JSON.stringify(e));
      }
    },
    //调机完成
    async finishRepair() {
      let _this = this;
      if (_this.faultType == "") {
        Dialog.alert({
          message: "请选择故障类型",
          theme: 'round-button',
        }).then(() => {
          // on close
        });
        return;
      }
      if (
        _this.equipRepairVo.audioList.length == 0 &&
        _this.equipRepairVo.videoList.length == 0 &&
        _this.equipRepairVo.picList.length == 0
      ) {
        Toast("请先拍照或录制视频或录制语音");
        return;
      }
      _this.btnLoading = true;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() + "/app/repair/repairInfo/finishRepair",
          {
            body: {
              recordId: _this.form.id,
              stopReson: _this.stopResonValue,
              faultType: _this.faultType,
              sparePart: _this.sparePart,
              finishTime: _this.getNowTime(),
              repairUser: _this.user.nickName,
              requestId: _this.form.requestId,
              siteKey: _this.user.site,
              remark: _this.equipRepairVo.remark,
            },
          }
        );
        //
        let res1 = await _httpClient.post(
          _httpClient.getApiPrefix() + "/app/repair/repairInfo/saveFile",
          {
            body: this.equipRepairVo,
          }
        );
        if (res.code === 200) {
          Dialog.alert({
            message: res.msg,
            theme: 'round-button',
          }).then(() => {
            // on close
          });
          _this.form.status = "3"; //更新状态为调机完成
          //调机完成，关闭当前页面
          api.sendEvent({
            name: "result",
            extra: {
              item: _this.form,
            },
          });
          api.closeWin();
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        alert(JSON.stringify(e));
        Toast.fail(JSON.stringify(e));
      }
    },
    //任务转派
    taskAssign() {
      let _this = this;
      Dialog.confirm({
        title: "提示",
        message: "是否确认转派任务?",
        theme: 'round-button',
      })
        .then(() => {
          //打开人员选择界面
          api.openWin({
            name: "personSelect",
            url: "./maintain-toRepair-personSelect.html",
            type: "fade",
          });
          api.addEventListener(
            {
              name: "resultUser",
            },
            function (ret, err) {
              if (ret.value.selectUser.length == 0) {
                Dialog.alert({
                  message: "未选择指派人员",
                  theme: 'round-button',
                }).then(() => {
                  // on close
                });
                return;
              }
              let selectUser = ret.value.selectUser;
              //更新执行人
              _this.updateExecuter(_this.form.id, selectUser[0].userAccound);
            }
          );
        })
        .catch(() => { });
    },

    //更新执行人
    async updateExecuter(id, userName) {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() +
          "/app/repair/repairInfo/updateRepairUser",
          {
            body: { id: id, repairUser: userName },
          }
        );
        if (res.code === 200) {
          Toast.clear();
          api.sendEvent({
            name: "result",
            extra: {
              item: _this.form,
            },
          });
          _this.$ac.closeWin();
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        Toast.clear();
        alert(JSON.stringify(e));
      }
    },

    //查询调机单协助人
    async queryHelpUser() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() + "/app/repair/repairInfo/selectHelpUser",
          {
            // body: { recordId: _this.form.id },
            body: { recordId: _this.form.id },
          }
        );
        if (res.code === 200) {
          Toast.clear();
          _this.helpUser = res.rows;
          if (_this.helpUser.length > 0) {
            for (let i = 0; i < _this.helpUser.length; i++) {
              switch (i) {
                case 0:
                  _this.helpUserStr += _this.helpUser[i].userCode;
                  _this.helpUserNameStr += _this.helpUser[i].userName;
                  break;
                default:
                  //默认显示三个
                  if (i < 4) {
                    _this.helpUserStr += "、" + _this.helpUser[i].userCode;
                    _this.helpUserNameStr += "、" + _this.helpUser[i].userName;
                  }
              }
            }
          }
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        Toast.clear();
        alert(JSON.stringify(e));
      }
    },

    //初始化查询故障信息
    async queryEmFault() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() + "/app/repair/repairInfo/selectEmFault",
          {
            body: { siteKey: _this.user.site },
          }
        );
        if (res.code === 200) {
          Toast.clear();
          _this.faultTypes = res.rows;
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        Toast.clear();
        alert(JSON.stringify(e));
      }
    },

    //初始化查询调机暂停原因
    async queryEmStopReson() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "Loading...",
      });
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() +
          "/system/dict/data/type/sys_repair_stop_reson"
        );
        if (res.code === 200) {
          Toast.clear();
          if (res.data.length > 0) {
            for (let i = 0; i < res.data.length; i++) {
              let reson = { text: res.data[i].dictValue };
              _this.stopReson.push(reson);
            }
          } else {
            _this.stopReson = [];
          }
        } else {
          Toast.fail(res.msg);
        }
      } catch (e) {
        Toast.clear();
        alert(JSON.stringify(e));
      }
    },

    // 获取当前时间
    getNowTime() {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      dateTime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
      return dateTime;
    },
  },
};
</script>
<style>
body {
  background-color: White;
}

.pic {
  width: 100%;
  text-align: center;
}

.clickbtn {
  width: 1;
  margin: 5px 10px;
  line-height: 45px;
  border: 1px #ccc dashed;
  text-align: center;
}

.buttonColor {
  background-color: #ff9933;
  border-color: #ff9933;
}

.buttonColor2 {
  background-color: #1076c0;
  border-color: #1076c0;
}

.message {
  font-size: 15px;
  margin-top: 5px;
  margin-left: -10px;
  font-weight: bold;
}

.divStyle {
  border: 1px solid #dcdcdc;
  border-top-width: 3px;
  border-left-width: 3px;
  margin-left: 5px;
  margin-right: 4px;
  margin-top: 4px;
}

.pagination {
  position: fixed;
  bottom: 15px;
  width: 300px;
  margin-left: 10%;
  color: rgb(0, 0, 255);
  text-align: center;
}

.divMedia {
  margin-top: 0px;
  height: 230px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
}

.divMedia2 {
  margin-top: 0px;
  height: 160px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
}

.button {
  position: fixed;
  left: 5px;
  right: 5px;
  bottom: 24px;
  width: 100%;
  height: 30px;
  font-size: 12px;
}

.divMessage {
  margin-top: 0px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
}

.Field {
  font-weight: bold;
  font-size: 15px;
  color: #9c8e8e;
  height: 45px;
}

.Field2 {
  font-weight: bold;
  font-size: 15px;
  color: #9c8e8e;

}

.textScroll {
  display: inline-block;
  position: fixed;
  top: 5rem;
  bottom: 0rem;
  overflow: scroll;
  right: 0;
  width: 100%;
  z-index: 1;
}
</style>
